package com.sinodata.bsm.cicp.collector.oracle;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.common.constants.SeparatorConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class SqlCollector extends AbstractJDBCCollector {
    /**
     * 
     * 
     * ResultSetMetaData 这个应该可以
     */

    @Override
    public List<ResPropertyValue> collect(String ip) {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        String dbtype = this.getParamMap().get("DBType");
        String dbname = this.paramMap.get("DBName");
        String sql = this.getParamMap().get("SQL");

        String[] rs = this.getStrings(dbtype, dbname, ip, sql);

        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        for (int i = 0; i < propMetas.length; i++) {
            propValues[i] = ResPropertyValue.createPropValue(rs[i], propMetas[i]);
        }

        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
        return values;
    }

    private String[] getStrings(String dbtype, String dbname, String ip, String sql) {
        String[] strs = new String[2];
        int numbercount = 0;
        try {
            ResultSet rs = this.getCachedResultSet(dbtype, ip, dbname, sql);
            if (rs != null) {
                StringBuilder sb = new StringBuilder("");

                ResultSetMetaData rsmd = rs.getMetaData();
                int column = rsmd.getColumnCount();
                for (int i = 0; i < column; i++) {
                    sb.append(rsmd.getColumnName(i + 1)).append(SeparatorConstants.COL_SEPARATOR);
                }
                sb.append(SeparatorConstants.LIN_SEPARATOR);

                while (rs.next()) {
                    numbercount++;
                    if (numbercount <= 200) {
                        String line = getTabLineStr(rs, column);
                        sb.append(line).append(SeparatorConstants.LIN_SEPARATOR);
                    }
                }
                strs[0] = numbercount + "";
                strs[1] = sb.toString();
            }
        } catch (Exception ex) {
            logger.error("while connect database : ", ex);
        }
        return strs;
    }

    private String getTabLineStr(ResultSet rst, int colNum) {
        StringBuffer tabLineStr = new StringBuffer();
        try {
            for (int i = 1; i <= colNum; i++) {
                String val = rst.getString(i);
                if (val == null || val.trim().length() == 0) {
                    val = " ";
                } else
                    val = val.trim();
                tabLineStr.append(val).append(SeparatorConstants.COL_SEPARATOR);
            }
        } catch (Exception e) {
            return null;
        }
        return tabLineStr.toString();
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "DBType", "DBName", "SQL" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "Statis_Num", "Show_Of_ResultSet" };
    }

}
